/*
 * @Author: xiaosihan 
 * @Date: 2022-04-07 16:10:06 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2022-06-20 17:16:14
 */

import { Sprite } from "three";
import threeLoader from "three-base/threeLoader";

/**
 *  图片标签
 *
 * @export
 * @class ImgSprite
 * @extends {Sprite}
 */
export default class ImgSprite extends Sprite {

    constructor(src: string, imgScale: number = 0.4) {
        super();

        this.imgScale = imgScale;

        this.setSrc(src);

        this.material.opacity = 0;
    }

    isImgSprite = true;

    // 标签有透明, 渲染顺序需要往后排
    renderOrder = 10;

    imgScale = 0.4;

    // 设置图片地址
    async setSrc(src: string) {
        const map = await threeLoader.getTexture(src);
        this.material.map = map;
        this.material.needsUpdate = true;

        while (!map.image) {
            await new Promise(resolve => setTimeout(resolve, 200));
        }
        const { naturalWidth, naturalHeight } = map.image;
        this.scale.set(naturalWidth / naturalHeight, 1, 1);
        this.scale.multiplyScalar(this.imgScale);
    }



}